<?php

$dbo = Database::getDBOinstance();

$default_combo = "";

if ($_GET['get_times']) {
	$str_dates = generate_days($_GET['day']);
	$day = $_GET['day'];
	$str_times = generate_times($day, $_GET['time']);
}
elseif ($_GET['get_list']) {
	//Populate the combo boxes
	$str_dates = generate_days($_GET['day']);
	$str_times = generate_times($day, $_GET['time']);
	
	//Get the list of subjects that fall in to the criteria
	$str_exams = generate_subjects($_GET['day'], $_GET['time']);	
}
elseif ($_GET['get_details']) {
    
	$subject_code = $_GET['select_exam'];
    $details = explode(' ', $_GET['select_exam']);
    $sub_code = $details[0];
    //Get subject details
    $result = $dbo->mysql_query("select * from sase_supervisor as ssup, supervises_subject as ss where ssup.sup_id = ss.sup_id and ss.sub_code = '$sub_code'");
    
    $supervisors = "";
    
    while ($row = mysql_fetch_assoc($result)) {
        $supervisors = $supervisors . ', ' . $row['sup_name'];
	    
    }
    
    $supervisors = substr($supervisors, 2);/**/

	$result = $dbo->mysql_query("select * from sase_subject where sub_code = '$sub_code'");
	$row = mysql_fetch_assoc($result);
	$hall_name = $row['sub_hall'];
	$sub_desc = $_GET['select_exam'];
    $str_dates = generate_days($_GET['day']);
}
elseif ($_GET['accept']){
	$details = explode(' ', $_GET['select_exam']);
	$sub_code = strtolower($details[0]);

    Supervisor::enrolSubject($sub_code, $user_id);
    
    
}
else {
	$str_dates = generate_days($_GET['day']);
}

function generate_days($start_day = NULL){
	//Just load the first combo box
    global $dbo;
	$query = 'select distinct sub_day from sase_subject';
	$result = $dbo->mysql_query($query);

	$dates = array();

	while ($row = mysql_fetch_assoc($result)){
		$dates[] = $row['sub_day'];
	}
	
	$str_dates = "";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>" . PHP_EOL;
	}
	
	if ($start_day) $str_dates = "<option value=\"".$start_day."\">".$start_day."</option>" . PHP_EOL . $str_dates;
	
	return $str_dates;
}

function generate_times($day, $start_time = NULL) {
    global $dbo;
	//times will hold the particular time values for the exam
	$times = array();
	
	//Get values of times for the given day
	$query = "select sub_start, sub_end from sase_subject where sub_day = '$day'";
	$result = $dbo->mysql_query($query);
	
	//put them in to the times array
	while ($row = mysql_fetch_assoc($result)) {
		$times[] = $row['sub_start'] . ' to ' . $row['sub_end'];	
	}
	
	$times = array_unique($times);
	sort($times);
	
	//generate option box optins html
	$str_times = "";
	foreach ($times as $v) {
        $str_times =  $str_times."<option value=\"".$v."\">".$v."</option>" . PHP_EOL;
    }
	
	if ($start_time) 
		$str_times = "<option value=\"".$start_time."\">".$start_time."</option>" . PHP_EOL . $str_times;
		
	return $str_times;
}

function generate_subjects ($day, $time) {
    global $dbo;
	$times = explode(' to ', $time);
	$query = "select * from sase_subject where sub_day = '$day' and sub_start >= '" . $times[0] . "' and sub_end <= '" . $times[1] . "' order by sub_start";
	//echo $query;
	$result = $dbo->mysql_query($query);
	
	$sub_str = "";
	while ($row = mysql_fetch_assoc($result)) {
		$sub_str = $sub_str . "<option value=\"" . $row['sub_code'] . "\">" . $row['sub_code'] . ' : ' . $row['sub_name'] . ' : ' . $row['sub_start'] . ' to ' . $row['sub_end'] . "</option>";
	}
	return $sub_str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome <?php echo $var_supname; ?></title>
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #CCC;
}

</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>



<body>

<div class="container">
	<?php include 'headder.page.php'; ?>
  
	<div class="sitebody">
    	<div class="left">
        
       	  <p>Select preferable exams...</p>
    	  
          <div class="search_box">
          	
            <form action="home.php" method="get">
            	<center>
                <h4>Preferable Day</h4>
                <select name="day" size="1">
                    <?php echo $str_dates; ?>
                </select>
                <input name="action" type="hidden" id="action" value="search" />
                <input type="submit" value="get times" name="get_times" />			
            	</center>
            </form>
        </div>
        
        <div class="search_box">
            <form action="home.php" method="get">
            	<center>
                	<h4>Preferable Time</h4>
                  <select name="time" size="1" id="time">
                        <?php echo $str_times; ?>
                    </select>
                  <input type="hidden" value="<?php echo $day; ?>" name="day" />
                    <input name="action" type="hidden" id="action" value="search" />
                    <input type="submit" value="get list" name="get_list" />
             	</center>
            </form>
    	</div>
        
        <div class="search_box">
        	
            <form action="home.php" method="get">
                <center>
                <h4>Preferable Subject</h4>
                <select name="select_exam" size="1">
                    <?php echo $str_exams; ?>
                </select>
                <input name="action" type="hidden" id="action" value="search" />
                <input name="get_details" type="submit" id="get_details" value="get details" />
            	</center>
            </form>
        </div>
        
        <div class="search_box">
        	<form action="home.php" method="get">
              <table width="95%" border="0">
                <tr>
                  <td align="right">Subject : </td>
                  <td><?php echo $sub_desc; ?></td>
                </tr>
                <tr>
                  <td width="43%" align="right">Exam Hall :</td>
                  <td width="57%"><?php echo $hall_name; ?></td>
                </tr>
                <tr>
                  <td align="right">Current supervisors :</td>
                  <td valign="top"><?php echo $supervisors; ?></td>
                </tr>
                <tr>
                  <td colspan="2" align="center"><input name="action" type="hidden" id="action" value="search" />
                  <input name="select_exam" type="hidden" id="select_exam" value="<?php echo $subject_code; ?>" /><input name="accept" type="submit" id="accept" value="accept" /></td>
                </tr>
              </table>
	</form>
        </div>
          
   	  </div>
</div>
    <div style="clear:both;"></div>
    <div class="footer">
    
    </div>
</div>


</body>
</html>